<?php

namespace App\Http\Controllers;

use App\Models\Consociation;
use App\Models\System\SysLog;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use App\Repositories\ConsociationRepository;
use App\Http\Requests\Logic\Consociation\StoreResource;
use App\Http\Requests\Logic\Consociation\QueryResource;
use App\Http\Requests\Logic\Consociation\UpdateResource;
use App\Http\Requests\Logic\Consociation\QueryCollection;

/**
 * 公司合作模块控制器
 *
 * Date: 22/03/2018
 * @author George
 * @package App\Http\Controllers
 */
class ConsociationController extends Controller
{
    protected $consociationRepository;

    /**
     * ConsociationController constructor.
     * @param ConsociationRepository $consociationRepository
     */
    public function __construct
    (
        ConsociationRepository $consociationRepository
    )
    {
        $this->consociationRepository = $consociationRepository;
    }

    /**
     * 获取合作公司列表
     * @param QueryCollection $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(QueryCollection $request)
    {
        $initiator = $request->get('initiator');
        $project = $request->get('project_id');
        $paginate = $request->get('paginate', 15);
        $status = $request->get('status');
        $query = $this->consociationRepository->getByCompany(Auth::user()->company_id, $initiator, $project)->getByStatus($status);
        return success($query->paginate($paginate));
    }


    /**
     * 获取合作公司条件
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function company(Request $request)
    {
        $company = $request->user()->company_id;
        $companies = Consociation::query()->where('partner',$company)->select('initiator')->with('initiator')->get()->unique();
        return success($companies);
    }


    /**
     * 获取详情信息
     * @param QueryResource $request
     * @param Consociation $consociation
     * @return \Illuminate\Http\JsonResponse
     */
    public function show(QueryResource $request,Consociation $consociation)
    {
        $consociation->initiator = $consociation->initiator()->get();
        $consociation->project_id = $consociation->project()->get();
        $consociation->partner = $consociation->partner()->get();
        return success($consociation);
    }


    /**
     * 创建申请合作公司
     * @param StoreResource $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\GeneralException
     */
    public function store(StoreResource $request)
    {
        $attributes = $this->extractInputFromRules($request, $request->rules());
        $attributes['initiator'] = $request->user()->company_id;
//        $attributes['permissions'] = json_encode($attributes['permissions'];
        $attributes['status'] = 'Unaudited';
        $consociation = $this->consociationRepository->create($attributes);
        SysLog::log(Auth::user(),'公司合作模块','申请合作公司');
        return stored($consociation);
    }

    /**
     * 修改合作公司状态
     * @param UpdateResource $request
     * @param Consociation $consociation
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(UpdateResource $request, Consociation $consociation)
    {
        $attributes = $this->extractInputFromRules($request, $request->rules());
        $consociation->update($attributes);
        SysLog::log(Auth::user(),'公司合作模块','修改合作公司状态');
        return updated(['修改成功']);
    }

    /**
     * 批量同意
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function updates(Request $request)
    {
        $data = json_decode($request->getContent(),true);
        $status = DB::table('consociations')->whereIn('id',$data)->update(['status'=>'Checked']);
        return updated($status);
    }
}
